<template>
  <el-dialog-com width="60%" title="添加用户"
  emitName="dialogClose" @dialogClose="dialogClose"
  checkName="addUserFormCheckFunc" @addUserFormCheckFunc="addUserFormCheckFunc"
  ref="addUserDialogRef">
    <template v-slot:main>
      
      <!-- Form表单 -->
      <el-form
      :model="roleAddUserInfo"
      :rules="rules"
      ref="roleAddUserInfoFormRef"
      label-width="100px">

        <!-- 用户名 -->
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="roleAddUserInfo.roleName"></el-input>
        </el-form-item>

        <!-- 邮箱 -->
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="roleAddUserInfo.roleDesc"></el-input>
        </el-form-item>

      </el-form>

    </template>
  </el-dialog-com>
</template>

<script>
// 引入组件
import ElDialogCom from '@/components/common/ElDialogCom';

// 网络
import { postAddRole } from '@/network/roleControlRequest';

export default {
  data() {
    return {
      roleAddUserInfo: {
        roleName: '',
        roleDesc: ''
      },
      rules: { // 表单校验规则
        roleName: [ // 邮箱规则
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
    },
  methods: {
    dialogClose() { // 关闭dialog组件后清空表单
      this.$refs['roleAddUserInfoFormRef'].resetFields();
    },
    async addUserFormCheckFunc() { // 提交事件触发验证 通过即 发送网络请求
      let flag = false; // 是否成功验证标志
      this.$refs['roleAddUserInfoFormRef'].validate(val => {
        if (!val) {
          flag = !flag;
          return this.$message.warning('请按照提示填写完整');
        }
      })
      if (flag) { 
        return;
      }

      // 发送网络请求
      const { meta } = await postAddRole(this.roleAddUserInfo);
      // console.log(data, meta);
      if (meta.status !== 201) {
        console.log(meta);
        this.$message.error(meta.msg);
        return this.$refs['addUserDialogRef'].$data.dialogVisible = false;
      }
      this.$bus.$emit('getRole');
      this.$message.success(meta.msg);
      // 修改公共组件的开启关闭属性
      return this.$refs['addUserDialogRef'].$data.dialogVisible = false;
    }
  },
  components: {
    ElDialogCom
  }
}
</script>

<style lang="scss" scoped>

</style>